define(function(require, exports, module) {
  var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  var jsonview = require('jsonview');

  var drag = require('hy5b/frontend/drag/index');
  require('css!hy5b/frontend/drag/drag.css');

  var auto = require('hy5b/auto/hauto');
  window.moment = require('moment');

  var num = 180; //生成点数
  var time_consecutive = true; //时间连续
  var baseMillisecondTimeStamp = moment('2016-12-24 00:13').valueOf()

  function sortNumber(a, b) {
    return a - b;
  }

  function autoMillisecondTimeStamps(num, step) {
    var time = parseInt(baseMillisecondTimeStamp);
    step = step || 10000; //10s
    var times = [];
    for (var i = 0; i < num; i++) {
      times.push(time + i * step);
    }
    return times;
  }

  if (!time_consecutive) {
    var timestamps = auto("$timestamp, 100000", num);
    timestamps.sort(sortNumber)
    timestamps = _.uniq(timestamps, true);
    timestamps = _.map(timestamps, function(timestamp) {
      return timestamp * 1000;
    })
  } else {
    var timestamps = autoMillisecondTimeStamps(num);
  }
  
  num = timestamps.length;

  var val1s = auto('$int, 1, 100', num);
  val1s.sort(sortNumber)

  var val2s = auto('$int, 1, 100', num);

  var newValue1s = _.map(timestamps, function(timestamp, index) {
    return {
      name: '',
      value: [timestamp, val1s[index]]
    }
  })

  var newValue2s = _.map(timestamps, function(timestamp, index) {
    return {
      name: '',
      value: [timestamp, val2s[index]]
    }
  })

  function app() {
    var echarts = require('npm/echarts/dist/echarts');
    var t = require('t');

    var line = require('../../../graph/option/line');

    line = t.deepCopy(line);
    line.xAxis.type = 'time';
    line.xAxis.interval = 600000; //10min
    line.xAxis.splitNumber = 6;
    line.xAxis.min = baseMillisecondTimeStamp + line.xAxis.interval;
    line.xAxis.max = baseMillisecondTimeStamp + num * 10000 + line.xAxis.interval;

    line.xAxis.data = null;
    line.series[0].data = newValue1s;
    line.series[1].data = newValue2s;

    var $div = $('<div>').css({
      'background-color': '#2f559c'
    }).width(800).height(600);

    $('#main').html($div);

    var $chart = $('<div id="chart">').width('100%').height('100%');

    $div.html($chart);

    var chart = echarts.init($chart[0]);

    chart.setOption(line);
  }

  return app;
});